﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.XWLW;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;

public partial class EditPM : System.Web.UI.Page
{
    /// <summary>
    /// 学位论文操作对象
    /// </summary>
    private XWLW_DAO xwlwDAO = new XWLW_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        //获取文件名信息
        String wjm = Request["wjm"];
        if (null == wjm || "".Equals(wjm))
        {
            message = "错误：文件名为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        XWLW_PM xwlwPM = this.xwlwDAO.GetXWLW_PMByWjm(wjm);
        XWLW_FL xwlwFL = null;
        if (null == xwlwPM)
        {
            message = "错误：没有找到此学位论文信息";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        xwlwFL = xwlwDAO.GetXWLW_FLByFlbm(xwlwPM.flbm);
        if (null == xwlwFL)
        {
            message = "错误：没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalXWLW.LISTPM_PAGE + "?catalog=" + xwlwPM.sybm;
       
        
        //分类编码
        this.flbmTextBox.Text = xwlwFL.flbm;
        //内容
        this.nrTextBox.Text = xwlwFL.nr;
        //索引编码
        this.sybmTextBox.Text=xwlwPM.sybm;
        //年份
        this.nfTextBox.Text = xwlwPM.nf.ToString();

        //中文题名
        this.zwtmTextBox.Text = xwlwPM.zwtm;
       
        //外文题名
        this.ywtmTextBox.Text =xwlwPM.ywtm;
        
        //作者
        this.zzTextBox.Text = xwlwPM.zz;
        //导师
        this.dsTextBox.Text = xwlwPM.ds;
        //论文级别
        this.lwjbTextBox.Text = xwlwPM.lwjb;
        //学科专业名称
        this.xkzymcTextBox.Text = xwlwPM.xkzymc;
        //分类号
        this.flhTextBox.Text = xwlwPM.flh;
        //学位授予单位
        this.xwsydwTextBox.Text = xwlwPM.xwsydw;
        //论文提交日期
        this.lwtjrqTextBox.Text = xwlwPM.lwtjrq.ToString();
        //论文答辩日期
        this.lwdbrqTextBox.Text = xwlwPM.lwdbrq.ToString();
        //学位授予日期
        this.xwsyrqTextBox.Text = xwlwPM.xwsyrq.ToString();
        //中文主题词
        this.zwztcTextBox.Text = xwlwPM.zwztc;
        //中文摘要
        this.zwzyTextBox.Text = xwlwPM.zwzy;
        //英文主题词
        this.ywztcTextBox.Text = xwlwPM.ywztc;
        //英文摘要
        this.ywzyTextBox.Text = xwlwPM.ywzy;
        //分类体系
        this.fltxTextBox.Text = xwlwPM.fltx;
        //原文件名
        //出版日期
        this.cbrqTextBox.Text = xwlwPM.cbrq.ToString();
        //页数
        this.ysTextBox.Text = xwlwPM.ys.ToString();
        //文件名
        this.wjmHyperLink.Text = wjm ;
        this.wjmHyperLink.NavigateUrl = GlobalResource.VIEW_PAGE+"?fileName="+wjm;

        //登录人
        this.dlrTextBox.Text = xwlwPM.dlr;

        User user = (User)Session["login"];
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
        }

        this.SetPreAndNextPM();
    }

    /// <summary>
    /// 获取用户填写的学位论文篇名信息
    /// </summary>
    /// <returns></returns>
    private XWLW_PM GetXWLW_PM()
    {
        XWLW_PM xwlwPM = new XWLW_PM();
        //分类编码
        xwlwPM.flbm = this.flbmTextBox.Text;
        //索引编码
        xwlwPM.sybm = this.sybmTextBox.Text;
        
        //文件名
        xwlwPM.wjm = this.wjmHyperLink.Text;
        //原文件名
        xwlwPM.ywjm = this.ywjmFileUpload.FileName;
        //中文题名
        xwlwPM.zwtm = this.zwtmTextBox.Text.Trim();
        //英文题名
        xwlwPM.ywjm = this.ywtmTextBox.Text.Trim();
        //作者
        xwlwPM.zz = this.zzTextBox.Text.Trim();
        //导师
        xwlwPM.ds = this.dsTextBox.Text.Trim();
        //论文级别
        xwlwPM.lwjb = this.lwjbTextBox.Text.Trim();
        //学科专业名称
        xwlwPM.xkzymc= this.xkzymcTextBox.Text.Trim();
        //分类号
        xwlwPM.flh = this.flhTextBox.Text.Trim();

        //学位授予单位
        xwlwPM.xwsydw = this.xwsydwTextBox.Text.Trim();
        //论文提交日期
        xwlwPM.lwtjrq = DateTime.Parse(this.lwtjrqTextBox.Text);
        //论文答辩日期
        xwlwPM.lwdbrq = DateTime.Parse(this.lwdbrqTextBox.Text);
        //学位授予日期
        xwlwPM.xwsyrq = DateTime.Parse(this.xwsyrqTextBox.Text);
        //中文主题词
        xwlwPM.zwztc = this.zwztcTextBox.Text.Trim();
        //中文摘要
        xwlwPM.zwzy = this.zwzyTextBox.Text.Trim();
        //英文主题词
        xwlwPM.ywztc = this.ywztcTextBox.Text.Trim();
        //英文摘要
        xwlwPM.ywzy = this.ywzyTextBox.Text.Trim();
        //年份
        xwlwPM.nf = Int32.Parse(this.nfTextBox.Text);
        //出版日期
        xwlwPM.cbrq = DateTime.Parse(this.cbrqTextBox.Text);
        //页数
        xwlwPM.ys = Int32.Parse(this.ysTextBox.Text);
        //更新日期
        xwlwPM.gxrq = DateTime.Now;
        //全文
        xwlwPM.qw = "1";
        //登录人
        xwlwPM.dlr = this.dlrTextBox.Text;

        return xwlwPM;

    }

    /// <summary>
    /// 获取下一个文件名，包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <returns></returns>
    private String GetWJM(String wjm)
    {
        String result = null;
        if (null == wjm || "".Equals(wjm))
        {
            result = this.sybmTextBox.Text + "-00000.PDF";
        }
        else
        {
            //获取文件名的后三位流水号，并在其基础上加1
            int endIndex = wjm.LastIndexOf(".");

            String phString = wjm.Substring(endIndex - 5, 5);

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "0000" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "000" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 4)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >4)
            {
                String message = "错误：篇名数量已经超过了100000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }

            result = this.sybmTextBox.Text + "-" + phString+".PDF";
        }
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateZWTM_AND_YWTM())
        {
            return;
        }
        if (!this.ValidateWJM())
        {
            return;
        }
        if (!this.ValidateLWTJRQ())
        {
            return;
        }
        if (!this.ValidateLWDBRQ())
        {
            return;
        }
        if (!this.ValidateXWSYRQ())
        {
            return;
        }
        if (!this.ValidateCBRQ())
        {
            return;
        }
        if (!ValidateYWJMFileUpload())
        {
            return;
        }

        if (null != this.ywjmFileUpload && null != this.ywjmFileUpload.FileName && !"".Equals(this.ywjmFileUpload.FileName))
        {
            if (!OperationFile.Upload(this.ywjmFileUpload, this.wjmHyperLink.Text))
            {
                Response.Write("<Script>alert('修改考察报告篇名信息失败')</Script>");
                return;
            }
        }

        XWLW_PM xwlwPM = this.GetXWLW_PM();

        if (this.xwlwDAO.UpdateXWLW_PM(xwlwPM,((User)Session["login"]).name))
        {
            Response.Write("<Script>alert('添加学位论文篇名信息成功')</Script>");
            this.NextPM();
        }
        else
        {
            Response.Write("<Script>alert('添加学位论文篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        XWLW_PM xwlwPM = this.xwlwDAO.GetXWLW_PMByWjm(this.wjmHyperLink.Text);
        if (null == xwlwPM)
        {
            this.errorLabel.Text = "错误：要修改的文件名不存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证论文提交日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateLWTJRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.lwdbrqTextBox.Text = this.lwdbrqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.lwdbrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：论文提交日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证论文答辩日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateLWDBRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.lwdbrqTextBox.Text = this.lwdbrqTextBox.Text.Trim();

        try
        {
            DateTime.Parse(this.lwdbrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：论文答辩日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证出版日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCBRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.cbrqTextBox.Text = this.cbrqTextBox.Text.Trim();

        try
        {
            DateTime.Parse(this.cbrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：出版日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证学位授予日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateXWSYRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.xwsyrqTextBox.Text = this.xwsyrqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.xwsyrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：学位授予日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证中英文题名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateZWTM_AND_YWTM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if((null==this.zwtmTextBox.Text||"".Equals(this.zwtmTextBox.Text.Trim()))&&(null==this.ywtmTextBox.Text||"".Equals(this.ywtmTextBox.Text.Trim())))
        {
            this.errorLabel.Text = "错误：中文题名和英文题名至少有一个不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            return flag;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysTextBox.Text = pdf.NumberOfPages.ToString();
                }

                pdf.Close();
            }
        }
        return flag;
    }
    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text.Trim());
    }
    protected void dsTextBox_TextChanged(object sender, EventArgs e)
    {
        this.dsTextBox.Text = GlobalMethod.Semicolon(this.dsTextBox.Text.Trim());
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text.Trim());
    }
    protected void ywztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.ywztcTextBox.Text = GlobalMethod.Semicolon(this.ywztcTextBox.Text.Trim());
    }

    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        String pre = this.GetDirectionPM(-1);
        if (null != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalXWLW.EDITPM_PAGE + "?wjm=" + pre;
        }

        //下一篇
        String next = this.GetDirectionPM(1);
        if (null != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalXWLW.EDITPM_PAGE + "?wjm=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        String wjm = this.GetDirectionPM(1);
        if (null == wjm)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalXWLW.EDITPM_PAGE + "?wjm=" + wjm);
        }
    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private String GetDirectionPM(int direction)
    {
        String result = null;
        //获取文件名的后三位流水号，并在其基础上加1
        int endIndex = this.wjmHyperLink.Text.LastIndexOf(".");

        //前缀
        String startSubString = this.wjmHyperLink.Text.Substring(0, endIndex - 5);
        String phString = this.wjmHyperLink.Text.Substring(endIndex - 5, 5);

        int ph = 0;

        try
        {
            ph = Int32.Parse(phString);
        }
        catch (Exception)
        {
        }

        //当前篇号+1或-1
        if (direction > 0)
        {
            ph++;
        }
        else
        {
            ph--;
        }

        phString = ph.ToString();
        //长度为1
        if (phString.Length == 1)
        {
            phString = "0000" + phString;
        }
        else if (phString.Length == 2)
        {
            phString = "000" + phString;
        }
        else if (phString.Length == 3)
        {
            phString = "00" + phString;
        }
        else if (phString.Length == 4)
        {
            phString = "0" + phString;
        }
        result = startSubString  + phString + ".PDF";
        XWLW_PM xwlwPM = this.xwlwDAO.GetXWLW_PMByWjm(result);
        if (null == xwlwPM)
        {
            return null;
        }
        return result;
    }
}